﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MusicAcademy
{
    public partial class frmReports : Form
    {
        public frmReports()
        {
            InitializeComponent();
        }

        private void frmReports_Load(object sender, EventArgs e)
        {
             if (UserManager.HasAccess(this.Name, "View") == false)
            {
                MessageBox.Show("شما اجازه ی دیدن این فرم را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Close();
            }

            cmbReport.SelectedIndex = 0;
        }

        private void btnShowReport_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.MusicAcademyConnectionString);
            SqlCommand com = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            string academyName;

            com.Connection = con;
            com.CommandType = CommandType.Text;
            com.CommandText = "SELECT academyName FROM Academy WHERE ID=@ID";
            com.Parameters.AddWithValue("@ID", Program.academyID);

            con.Open();
            academyName = com.ExecuteScalar().ToString();
            com.Parameters.Clear();
            con.Close();

            da.SelectCommand = new SqlCommand();
            da.SelectCommand.Connection = con;
            da.SelectCommand.CommandType = CommandType.Text;
            da.SelectCommand.CommandText = "SELECT * FROM StudentsTermsReport AS Command";

            con.Open();
            da.Fill(dt);
            con.Close();

            switch (cmbReport.SelectedIndex)
            {
                case 0:
                    StudentsTerms1.SetDataSource(dt);
                    StudentsTerms1.SetParameterValue("AcademyName", academyName);
                    crystalReportViewer1.ReportSource = StudentsTerms1;
                    crystalReportViewer1.Refresh();
                    break;
            }
        }
    }
}
